package com.hk.servlet;

import com.hk.model.*;
import com.hk.service.*;
import com.hk.service.impl.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: KangWang
 * Date: 2021/5/17 10:20
 * State：Servlet 数据请求转发 控制层
 */
@WebServlet("/home")
public class HomeServlet extends HttpServlet {

    INavService iNavService = new NavServiceImpl();
    ICompanyProfileService iCompanyProfileService = new CompanyProfileServiceImpl();
    IImageService iImageService = new ImageServiceImpl();
    IProductDisplayService iProductDisplayService = new ProductDisplayServiceImpl();
    IDisplayService iDisplayService = new DisplayServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        设置UTF-8
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        获取项目路径
        String path = req.getServletContext().getRealPath("");
//        System.out.println("项目路径："+path);
        HttpSession session = req.getSession();
//        获取导航栏数据
        List<Nav> navs = iNavService.findAllNav(path);
//        获取公司简介数据
        CompanyProfile companyProfile = iCompanyProfileService.findAllCompanyProfile(path);
//        获取公共图片
        List<Image> images = iImageService.findAllImage(path);
//        获取产品首页概念
        ProductDisplay productDisplay = iProductDisplayService.findAllProductDisplay(path);
//        获取产品图片
        List<String> displayImg = iDisplayService.findAllDisplay(path).stream().map(Display::getProductImg).collect(Collectors.toList());
//        存储
        session.setAttribute("navs", navs);
        session.setAttribute("companyProfile", companyProfile);
        session.setAttribute("images", images);
        req.setAttribute("productDisplay", productDisplay);
        req.setAttribute("displayImg", displayImg);

//        页面转发
        req.getRequestDispatcher("/jsp/home.jsp").forward(req, resp);
//        页面重定向
//        resp.sendRedirect("/jsp/home.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
